package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.mapper.MemberMapper;
import com.itcast.mapper.OrderMapper;
import com.itcast.mapper.OrdersettingMapper;
import com.itcast.pojo.Member;
import com.itcast.pojo.Order;
import com.itcast.pojo.OrderSetting;
import com.itcast.untils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersettingMapper ordersettingMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 预约
     * @param map
     * @return
     */
    @Override
    public Result addUser(Map map) {
//        检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行 预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting;
        try {
            Date date = DateUtils.parseString2Date(orderDate);
            orderSetting = ordersettingMapper.findDate(date);
            if (orderSetting == null) {
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
            //检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if (number <= reservations) {
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
//            检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约 则无法完成再次预约
            //通过输入的手机号码获取该用户
            String telephone = (String) map.get("telephone");
            Member member = memberMapper.find(telephone);
            if (member != null) {
                //会员id
                Integer id = member.getId();
                //预约套餐的id
                String setmealId =  (String)map.get("setmealId");
                Order order = new Order(id, date,Integer.parseInt(setmealId));
                List<Order> orders = orderMapper.find(order);
                if (orders.size() > 0 && orders != null) {
                    return new Result(false, MessageConstant.ORDERSETTING_FAIL);
                }
            } else {
                //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
                member = new Member();
                member.setName((String) map.get("name"));
                member.setPhoneNumber(telephone);
                member.setIdCard((String) map.get("idCard"));
                member.setSex((String) map.get("sex"));
                member.setRegTime(new Date());
                memberMapper.add(member);//自动完成会员注册
            }
            //5、预约成功，更新当日的已预约人数
            Order order = new Order();
            order.setMemberId(member.getId());//设置会员ID
            order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
            order.setOrderType((String) map.get("orderType"));//预约类型
            order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
            orderMapper.add(order);
            //预约人数加一
            int num = orderSetting.getReservations() + 1;
            orderSetting = new OrderSetting(date, num);
            ordersettingMapper.set(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS,order.getId());

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 查询套餐预约信息
     * @param id
     */
    @Override
    public Map findById(Integer id) {
        Map map = orderMapper.findById(id);
        if(map != null){
            Date orderDate = (Date)map.get("orderDate");
            try {
                map.put("orderDate",DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 判断是否是会员
     * @param telephone
     * @return
     */
    @Override
    public Member find(String telephone) {
        return memberMapper.find(telephone);
    }

    /**
     * 添加会员
     * @param member
     */
    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }
}


